using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [PersistChildren(true)]
    [ParseChildren(false)]
    [ToolboxData("<{0}:Columns runat=server></{0}Columns>")]
    [Designer(typeof(ColumnsDesigner))]
    public class Columns : WebControl
    {
        private bool _IncludeShadow = true;

        public bool IncludeShadow
        {
            get { return _IncludeShadow; }
            set { _IncludeShadow = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //find number of columns child controls
            int noOfColumns = 0;
            foreach(Control c in this.Controls)
            {
                if(c is BaseColumn && c.Visible)
                {
                    noOfColumns = noOfColumns + ((BaseColumn)c).GetColumnWidth();
                }
            }

            if (noOfColumns > 0)
            {
                if (IncludeShadow)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "Shadow");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "TopRight");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();//TopRight

                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "Main");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                }

                if (noOfColumns == 3)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "ThreeColumns");
                }
                else if (noOfColumns == 2)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "TwoColumns");
                }
                else if (noOfColumns == 1)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "OneColumn");
                }
                else
                {
                    throw new ApplicationException(string.Format("Control '{0}' has incorrect number of colums. A Max of three is allowed. Currently there are '{1}'", this.ID, noOfColumns));
                }

                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                
                RenderChildren(writer);

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "ColumnsEnd");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderEndTag();//ColumnsEnd

                writer.RenderEndTag();//Columns

                if (IncludeShadow)
                {
                    writer.RenderEndTag();//Main

                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "Footer");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "Left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();//Left

                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "Right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();//Right

                    writer.RenderEndTag();//Footer

                    writer.RenderEndTag();//Shadow
                }
            }
        }
    }

    public class ColumnsDesigner : ContainerControlDesigner
    {
        public override string FrameCaption
        {
            get { return "Acms Columns"; }
        }

        private Style _Style;
        public override Style FrameStyle
        {
            get
            {
                if (_Style == null)
                {
                    _Style = new Style();
                    _Style.Width = Unit.Percentage(90);
                    _Style.Font.Name = "Verdana";
                    _Style.Font.Size = new FontUnit("Small");
                    _Style.BackColor = Color.LightGray;
                    _Style.ForeColor = Color.Black;
                }
                return _Style;
            }
        }
    }
}
